package org.tarena.dang.action.user;

import java.text.SimpleDateFormat;

import org.tarena.dang.action.BaseAction;
import org.tarena.dang.entity.User;
import org.tarena.dang.service.UserService;
import org.tarena.dang.service.UserServiceImp;
import org.tarena.dang.util.EncryptUtil;

public class LoginAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	private User user;
	private String code;

	public String execute() throws Exception {
		UserService userService = new UserServiceImp();
		this.user = userService.login(user);
		if(user.getEmailVerifyCode()!=null){
			code = EncryptUtil.Base64Encrypt((user.getEmailVerifyCode()+"-"+user.getId()).getBytes());
			System.out.println("checkcod_______---"+code);
			session.put("checkCode", code);
			System.out.println("**"+code);
			if(user.isEmailVerify()){
//				HttpSession session = ServletActionContext.getRequest().getSession(); 
//				session.setAttribute("d_user", user);
				session.put("nickname", user.getNickname());
				session.put("d_user", user);
				session.put("lasttime", new SimpleDateFormat("MM/dd HH:mm:ss").format(user.getLastLoginTime()));
				user.setLastLoginTime(System.currentTimeMillis());
				userService.update(user);
				
				return "success";
			
			}
			
			return "checkcode";
		}
		session.put("loginerror", "用户名或密码错误!");
		
		return "error";
	}
	
	public String login2(){
		return "success";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	
	
	
}
